﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Linq;

/// <summary>
///ZNXX 的摘要说明
/// </summary>
public partial class ZNXX
{
    public enum statusXX { 未读 = 1, 已读 }
    public enum ZXXXType { 后台 = 1, 前台 = 2 }
    public static ZNXX GetByID(int id)
    {

        using (DataClassesDataContext context = new DataClassesDataContext())
        {
            return context.ZNXX.FirstOrDefault(row => row.ID == id && row.Enable_Flag == 1);
        }
    }

    public static List<ZNXX> GetAll()
    {
        using (DataClassesDataContext context = new DataClassesDataContext())
        {
            return context.ZNXX.Where(row => row.Enable_Flag == 1).ToList();
        }
    }

    public void Add()
    {
        using (DataClassesDataContext context = new DataClassesDataContext())
        {
            context.ZNXX.InsertOnSubmit(this);
            context.SubmitChanges();
        }
    }

    public static void AddAll(List<ZNXX> lstZNXX)
    {
        using (DataClassesDataContext context = new DataClassesDataContext())
        {
            context.ZNXX.InsertAllOnSubmit(lstZNXX);
            context.SubmitChanges();
        }
    }


    public void Update()
    {
        using (DataClassesDataContext context = new DataClassesDataContext())
        {
            this.GetType().GetMethod("Detach").Invoke(this, null);
            var tbl = context.GetTable<ZNXX>();
            tbl.Attach(this);
            context.Refresh(RefreshMode.KeepCurrentValues, this);
            context.SubmitChanges();
        }
    }

    public static void Delete(int id)
    {
        using (DataClassesDataContext context = new DataClassesDataContext())
        {
            var objItem = context.ZNXX.FirstOrDefault(row => row.ID == id);
            if (objItem == null)
                return;
            context.ZNXX.DeleteOnSubmit(objItem);
            context.SubmitChanges();
        }
    }
    public static List<ZNXX> GetAllInUserIDByConditon(int SendUserID, int GetUserID, string AddTimeForm, string AddTimeTo, int statusXX, string Key, int type)
    {
        using (DataClassesDataContext context = new DataClassesDataContext())
        {
            var query = from t in context.ZNXX
                        where t.Enable_Flag == 1 && t.SendUserID == SendUserID
                        select t;
          
            if (GetUserID != 0)
                query = query.Where(row => row.GetUserID == GetUserID);
            if (!string.IsNullOrEmpty(AddTimeForm))
                query = query.Where(row => row.SendTime >= DongDong.Common.Misc.ConvertToDateTime(AddTimeForm));
            if (!string.IsNullOrEmpty(AddTimeTo))
                query = query.Where(row => row.SendTime <= DongDong.Common.Misc.ConvertToDateTime(AddTimeTo));
            if (!string.IsNullOrEmpty(Key))
                query = query.Where(row => row.Des.Contains(Key));
            if (statusXX != 0)
                query = query.Where(row => row.Status == statusXX);

            if (type != 0)
            {
                query = query.Where(row => row.Type == type);
            }
            query = query.OrderByDescending(row => row.SendTime);
            return query.ToList();
        }
    }
    public static List<ZNXX> GetAllByConditon(int SendUserID, int GetUserID, string AddTimeForm, string AddTimeTo, int statusXX, string Key, int type)
    {
        using (DataClassesDataContext context = new DataClassesDataContext())
        {
            var query = from t in context.ZNXX
                        where t.Enable_Flag == 1
                        select t;
            if (SendUserID != 0)
                query = query.Where(row => row.SendUserID == SendUserID);
            if (GetUserID != 0)
                query = query.Where(row => row.GetUserID == GetUserID);
            if (!string.IsNullOrEmpty(AddTimeForm))
                query = query.Where(row => row.SendTime >= DongDong.Common.Misc.ConvertToDateTime(AddTimeForm));
            if (!string.IsNullOrEmpty(AddTimeTo))
                query = query.Where(row => row.SendTime <= DongDong.Common.Misc.ConvertToDateTime(AddTimeTo));
            if (!string.IsNullOrEmpty(Key))
                query = query.Where(row => row.Des.Contains(Key));
            if (statusXX != 0)
                query = query.Where(row => row.Status == statusXX);

            if (type != 0)
            {
                query = query.Where(row => row.Type == type);
            }
            query = query.OrderByDescending(row => row.SendTime);
            return query.ToList();
        }
    }

    public static void BadDelete(int ID)
    {
        using (DataClassesDataContext context = new DataClassesDataContext())
        {
            ZNXX objZNXX = ZNXX.GetByID(ID);
            if (objZNXX == null)
                return;
            objZNXX.Enable_Flag = 0;
            objZNXX.Update();
        }
    }
}